/**
 * Copyright (c) <2010>, <SUNNY GUPTA>
 * All rights reserved.

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the <ORGANIZATION> nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <SUNNY GUPTA> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.gwtp.investmentadvisor.client.gui.financialsecuritysearch;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.SuggestOracle.Response;
import com.google.inject.Inject;
import com.gwtp.investmentadvisor.client.common.ServerQuerySuggestOracle;
import com.gwtp.investmentadvisor.client.common.ServerQuerySuggestOracle.SearchServer;
import com.gwtp.investmentadvisor.client.resources.AppResources;

/**
 * @author Sunny Gupta
 *
 */
public class FinancialSecuritySearchBoxView 
    extends Composite implements FinancialSecuritySearchBoxPresenter.Display {

  /**
   * UiBinder Interface Code.
   */
  private static FinancialSecuritySearchBoxViewUiView uiBinder = GWT
      .create(FinancialSecuritySearchBoxViewUiView.class);

  interface FinancialSecuritySearchBoxViewUiView extends UiBinder<Widget, FinancialSecuritySearchBoxView> {
  }

  /**
   * We want to instantiate the SuggestBox with an injected SuggestOracle.
   */
  @UiField(provided = true)
  SuggestBox searchTextBox;
  
  @UiField(provided = true)
  Image waitAnimation;
  
  @UiField(provided = true)
  AppResources resources;
  
  private final ServerQuerySuggestOracle oracle;
  
  @Inject
  public FinancialSecuritySearchBoxView(final ServerQuerySuggestOracle oracle) {
    // Provide resources
    this.resources = AppResources.INSTANCE;
    
    // Initialize SuggestBox with injected SuggestOracle
    this.oracle = oracle;
    this.searchTextBox = new SuggestBox(this.oracle);
    
    // Initialize wait animation
    this.waitAnimation = new Image(AppResources.INSTANCE.waitAnimation());
    // Hide by default
    this.waitAnimation.setVisible(false);
    
    // Initialize widgets
    initWidget(uiBinder.createAndBindUi(this));
  }

  /* (non-Javadoc)
   * @see com.gwt.investmentadvisor.client.presenter.FinancialSecuritySearchBoxPresenter.Display#getSearchText()
   */
  @Override
  public HasValue<String> getSearchText() {
    return this.searchTextBox.getTextBox();
  }

  /* (non-Javadoc)
   * @see com.gwt.investmentadvisor.client.presenter.FinancialSecuritySearchBoxPresenter.Display#getSearchTextChangeSender()
   */
  @Override
  public HasChangeHandlers getSearchTextChangeSender() {
    return this.searchTextBox.getTextBox();
  }

  /* (non-Javadoc)
   * @see net.customware.gwt.presenter.client.widget.WidgetDisplay#asWidget()
   */
  @Override
  public Widget asWidget() {
    // The View extends Composite and hence is a Widget
    return this;
  }

  /* (non-Javadoc)
   * @see com.gwt.investmentadvisor.client.presenter.FinancialSecuritySearchBoxPresenter.Display#getSuggestBox()
   */
  @Override
  public SuggestBox getSuggestBox() {
    return this.searchTextBox;
  }

  /* (non-Javadoc)
   * @see com.gwt.investmentadvisor.client.presenter.FinancialSecuritySearchBoxPresenter.Display#configureSearchServer(com.gwt.investmentadvisor.client.common.ServerQuerySuggestOracle.SearchServer)
   */
  @Override
  public void configureSearchServer(SearchServer searchServer) {
    this.oracle.configureSearchServer(searchServer);
  }

  @Override
  public void setDefaultResponse(Response defaultResponse) {
    this.oracle.setDefaultResponse(defaultResponse);
  }

  @Override
  public void addToSlot(Object slot, Widget content) {
    // TODO Auto-generated method stub
  }

  @Override
  public void removeFromSlot(Object slot, Widget content) {
    // TODO Auto-generated method stub
  }

  @Override
  public void setInSlot(Object slot, Widget content) {
    // TODO Auto-generated method stub
  }
}
